package org.budo.maven.plugin.assembly.mojo;

import lombok.Getter;
import lombok.Setter;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.project.MavenProject;
import org.budo.maven.plugin.assembly.mojo.processor.BudoMavenAssemblyLauncherProcessor;
import org.budo.maven.plugin.assembly.mojo.processor.BudoMavenAssemblyProcessor;
import org.budo.maven.plugin.assembly.mojo.processor.BudoMavenAssemblySpringBootProcessor;
import org.budo.maven.plugin.assembly.mojo.processor.BudoMavenAssemblyWarProcessor;
import org.budo.support.lang.util.StringUtil;

import java.util.Set;

/**
 * maven 打包插件
 *
 * @author lmw
 * @see org.apache.maven.plugins.dependency.fromConfiguration.UnpackMojo
 * @see org.apache.maven.plugins.assembly.mojos.SingleAssemblyMojo
 */
@Mojo( //
        name = "budo-assembly", //
        defaultPhase = LifecyclePhase.PACKAGE, //
        threadSafe = true, //
        requiresDependencyResolution = ResolutionScope.COMPILE //
)
@Getter
@Setter
public class BudoMavenAssemblyMojo extends AbstractMojo {
    @Parameter(defaultValue = "${project}", readonly = true, required = true)
    private MavenProject project;

    @Parameter(defaultValue = "${session}", readonly = true, required = true)
    private MavenSession session;

    @Parameter
    private String format = "tar"; // tar.gz tar zip

    @Override
    public void execute() throws MojoExecutionException, MojoFailureException {
        this.getLog().info("#51 BudoMavenAssemblyMojo#execute project.artifact=" + this.getProject().getArtifact() //
                + ", project.base=" + this.getProject().getBasedir() + ", format=" + this.getFormat() + ", this=" + this);

        String packaging = this.getProject().getPackaging();
        Boolean hasSpringBoot = this.artifactContains("org.springframework.boot", "spring-boot-starter-tomcat") || this.artifactContains("org.springframework.boot", "spring-boot-starter-undertow");
        Boolean hasBudoLauncher = this.artifactContains("org.budo", "budo-launcher");

        BudoMavenAssemblyProcessor processor = null;

        if ("war".equalsIgnoreCase(packaging)) {
            processor = new BudoMavenAssemblyWarProcessor(this.getProject(), this.getSession(), this.getFormat(), this.getLog());
        } else if (hasSpringBoot && !hasBudoLauncher) {
            processor = new BudoMavenAssemblySpringBootProcessor(this.getProject(), this.getSession(), this.getFormat(), this.getLog());
        } else if (hasBudoLauncher && !hasSpringBoot) {
            processor = new BudoMavenAssemblyLauncherProcessor(this.getProject(), this.getSession(), this.getFormat(), this.getLog());
        } else {
            throw new MojoExecutionException("#89 BudoMavenAssemblyMojo Error, project=" + this.getProject() + ", packaging=" + packaging + ", hasSpringBoot=" + hasSpringBoot + ", hasBudoLauncher=" + hasBudoLauncher);
        }

        try {
            processor.execute();
        } catch (Throwable e) {
            this.getLog().error("#88 doExecute error, e=" + e, e);
            throw new MojoExecutionException("#89 BudoMavenAssemblyMojo Error, e=" + e, e);
        }
    }

    private Boolean artifactContains(String groupId, String artifactId) {
        Set<Artifact> artifacts = this.getProject().getArtifacts();
        for (Artifact artifact : artifacts) {
            if (StringUtil.equals(groupId, artifact.getGroupId()) //
                    && StringUtil.equals(artifactId, artifact.getArtifactId())) {
                this.getLog().warn("#80 artifactContains, artifact=" + artifact + ", project" + this.getProject());

                return true;
            }
        }

        return false;
    }
}
